package com.novoda.runbuddy.activity;

import java.util.List;

import android.app.Activity;
import android.app.ListActivity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Button;

import com.google.android.maps.MapActivity;
import com.google.android.maps.MapView;
import com.novoda.runbuddy.R;
import com.novoda.runbuddy.controller.IOptionsMenu;
import com.novoda.runbuddy.controller.IRouteRunnerController;
import com.novoda.runbuddy.controller.OptionsMenuImpl;
import com.novoda.runbuddy.controller.RouteRunnerControllerImpl;
import com.novoda.runbuddy.model.ListOfRuns;
import com.novoda.runbuddy.model.Route;
import com.novoda.runbuddy.provider.ActivityID;
import com.novoda.runbuddy.service.RemoteStopClockServiceInterface;

public class RouteRunner extends ListActivity {
	private IRouteRunnerController	routeRunnerController;
	private IOptionsMenu			optionsMenu;
	private List<Route>					allRoutesAroundCurrentArea;

	@Override
	protected void onCreate(Bundle icicle) {
		super.onCreate(icicle);
		setContentView(R.layout.routerunner);
		optionsMenu = new OptionsMenuImpl(this);
		routeRunnerController = new RouteRunnerControllerImpl(this);
		allRoutesAroundCurrentArea = routeRunnerController.getRoutesAroundCurrentLocation();

		routeRunnerController.createRouteRunnerView(this, allRoutesAroundCurrentArea, (Button) findViewById(R.id.rerun_route_start));
	}

	public Context getContext() {
		return this;
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		super.onCreateOptionsMenu(menu);
		optionsMenu.addAllActivitiesToMenu(menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		optionsMenu.selectItem(item);
		return super.onOptionsItemSelected(item);
	}

	@Override
	protected void onStop() {
		super.onStop();
		RemoteStopClockServiceInterface.getInstance().unregisterListener(this);
	}

	@Override
	protected void onStart() {
		super.onStart();
		RemoteStopClockServiceInterface.getInstance().registerListener(this);
	}

	@Override
	protected void onResume() {
		super.onResume();
	}

	@Override
	protected void onPause() {
		super.onPause();
	}

	public static void show(Activity activity) {
		// Only start new activity if we're currently on a different activity
		if (activity.getLocalClassName().compareTo("activity.RouteRunner") != 0) {
			Intent intent = new Intent(activity, TabsAdapter.class);
			Bundle extras = new Bundle();
			extras.putString("name0", "RouteRunner");
			extras.putString("tab0", "RouteRunner");
			extras.putString("name1", "Location");
			extras.putString("tab1", "CurrentRunMap");
			extras.putInt("default", 0);
			intent.putExtras(extras);
			activity.startActivityForResult(intent, ActivityID.ROUTERUNNER);
		}
	}

}
